﻿@using Db8
@model IEnumerable<Db8.Data.User>
@{
    ViewBag.Title = "Users";
}

<section class="content">
<div>@Html.Partial("_Navigation")</div>

@using (Html.BeginForm()) {
<table>
    <tr>
        <th>Users</th>
        <th colspan="2">Roles</th>
    </tr>
    @foreach (var item in Model) {
        <tr>
            <td style="width: 360px;">
                <div>
                    <img src="@item.Picture" width="36" height="36" style="float:left; margin-right: 10px" /><a href="@item.Link" target="_blank"><strong>@item.Name</strong></a><br />
                    <small style="font-size: smaller;">Last Seen: <span class="datetime">@item.LastActivityDt</span></small>
                </div>
            </td>
            <td>
                <input type="checkbox" name="IsContentAdministrator" data-userid="@item.UserId" data-role="@Db8Roles.CONTENT_ADMINISTRATOR" id="chkca-@item.UserId" value="true" class="styled" @if(item.IsContentAdministrator) { <text>checked="checked"</text> } />
                <label for="chkca-@item.UserId" class="editor-field">Content Admin</label>
            </td>
            <td>
                <input type="checkbox" name="IsSystemAdministrator" data-userid="@item.UserId" data-role="@Db8Roles.SYSTEM_ADMINISTRATOR" id="chksa-@item.UserId" value="true" class="styled" @if(item.IsSystemAdministrator) { <text>checked="checked"</text> } />
                <label for="chksa-@item.UserId" class="editor-field">System Admin</label>
            </td>
        </tr>
    }
</table>
}
</section>
@section scripts {
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/db8-ui.js")"></script>
<script>
    $("[name='IsContentAdministrator'], [name='IsSystemAdministrator']").click(function () {

        //"SetIsInRole(long user, string role, bool isInRole)"
        var args = { user: $(this).attr("data-userid"), role: $(this).attr("data-role"), isInRole: $(this).is(":checked") };
        //alert("SetIsInRole(" + args.user + ", " + args.role + ", " + args.isInRole + ")");
        $.post("@Url.Action("SetIsInRole", "Admin")", args, function (responseText, textStatus) {
            if (textStatus != "success") {
                alert("An error occurred: " + textStatus)
            }
        });


    });
</script>
}
